/**
 *
 */
package com.wedroid.r2d2.support.response;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.wedroid.r2d2.support.BaseRequest;

/**
 * @author lute
 *
 */
public class RedirectURIResponse implements DroidResponse {

    private static final String REGEX = "<skey>(\\S+?)</skey><wxsid>(\\S+?)</wxsid><wxuin>(\\S+?)</wxuin><pass_ticket>(\\S+?)</pass_ticket>";

    private BaseRequest baseRequest;

    private String passTicket;

    private Pattern pattern;

    /**
     *
     */
    public RedirectURIResponse() {
        this.pattern = Pattern.compile(REGEX);
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.support.response.DroidResponse#parse(java.lang.String)
     */
    public void parse(String response) {
        Matcher matcher = pattern.matcher(response);
        if (matcher.find()) {
            String skey = matcher.group(1);
            String wxsid = matcher.group(2);
            String wxuin = matcher.group(3);
            this.baseRequest = new BaseRequest(wxsid, wxuin, skey);
            this.passTicket = matcher.group(4);
        }
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.support.response.DroidResponse#isSuccess()
     */
    public boolean isSuccess() {
        return StringUtils.isNotBlank(passTicket) &&
                baseRequest != null && baseRequest.isValid();
    }

    /**
     * @return the passTicket
     */
    public String getPassTicket() {
        return passTicket;
    }

    /**
     * @return the baseRequest
     */
    public BaseRequest getBaseRequest() {
        return baseRequest;
    }

}
